package com.xu.boot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

// @RestController = @Controller + @ResponseBody
@RestController
public class ParamController {

    @GetMapping("/car/{id}/owner/{username}")
    public Map<String, Object> getCar(@PathVariable("id") Integer id,
                                      @PathVariable("username") String name,
                                      @PathVariable Map<String, String> pv,
                                      @RequestHeader("Host") String host,
                                      @RequestHeader Map<String, String> headers,
                                      @RequestParam("age") int age,
                                      @RequestParam("hobbies") List<String> hobbies,
                                      @RequestParam Map<String, String> params){

        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("name", name);
        map.put("pv", pv);
        map.put("host", host);
        map.put("age", age);
        map.put("hobbies", hobbies);
        map.put("params", params);

        return map;
    }

    @PostMapping("/save")
    public Map<String, Object> save(@RequestBody String content){
        Map<String, Object> map = new HashMap<>();
        map.put("content", content);
        return map;
    }

    // 1. 语法 /cars/sell;low=34;brand=byd,audi,yd
    // 2. SpringBoot 默认是禁用了矩阵变量的功能
    //          手动开启：原理，对于路径的处理，UrlPathHelper 进行处理，UrlPathHelper 中有一个属性
    //          removeSemicolonContent（是否移除分号之后的内容）默认为 true
    // 3. 矩阵变量必须有 url 路径变量才能被解析
    @GetMapping("/cars/{path}")
    public Map<String, Object> carsSell(@MatrixVariable("low") Integer low,
                                        @MatrixVariable("brand") List<String> brand,
                                        @PathVariable("path") String path){
        Map<String, Object> map = new HashMap<>();
        map.put("low", low);
        map.put("brand", brand);
        map.put("path", path);
        return map;
    }

    @GetMapping("/boss/{bossId}/{empId}")
    public Map<String, Object> boss(@MatrixVariable(value = "age", pathVar = "bossId") Integer bossAge,
                                    @MatrixVariable(value = "age", pathVar = "empId") Integer empAge){
        Map<String, Object> map = new HashMap<>();
        map.put("bossId", bossAge);
        map.put("empId", empAge);
        return map;
    }

}
